package com.ymatou.auth.testcase;

import java.util.ArrayList;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.auth.parameter.LockUserRequestDtoBean;
import com.ymatou.auth.parameter.LoginAuthBean;
import com.ymatou.auth.service.LockUserRequestDtoCall;
import com.ymatou.auth.service.LoginAuthCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.UserCallService;
import com.ymttest.database.mongo.UserTokenWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.ECode;
/**
*锁定用户接口
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_LockUserRequestDto{
	private static LockUserRequestDtoBean lockuserrequestdtoBean;
	private static LockUserRequestDtoCall lockuserrequestdtoCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("锁定用户接口");
	}

	@Before
	public void caseUp() {
		lockuserrequestdtoBean=new LockUserRequestDtoBean();
		lockuserrequestdtoCall=new LockUserRequestDtoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(lockuserrequestdtoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_LockUserRequestDto_001() {
		Logger.start(true,"锁定用户,验证用户登录提示用户已锁定");
		try {			
			Logger.comment("注册一个新用户");
			String ip=TestDataManager.getRandomIp();
			String email=TestDataManager.getEmail();
			String phone=TestDataManager.getPhoneNum();
			Map UserMap =UserCallService.registerUser(email, phone, ip);
			
			String AccessToken = (String) UserMap.get("token");
			int UserId = (Integer) UserMap.get("userId");
			String UserName = (String) UserMap.get("loginId");
			ArrayList<Integer> users=new ArrayList<Integer>();
			users.add(UserId);
			Logger.comment("锁住用户");
			lockuserrequestdtoBean.setUserId(users);
			lockuserrequestdtoBean.setDescription("autotest");
			lockuserrequestdtoBean.setOperating("auto");
			lockuserrequestdtoCall.setData(lockuserrequestdtoBean);
			lockuserrequestdtoCall.callService();			
			//登录
			Logger.comment("开始登陆，验证用户被锁定");
			LoginAuthBean lab = new LoginAuthBean();
			LoginAuthCall lac = new LoginAuthCall();
			lab.setLoginId(UserName);
			lab.setPas("123456a");
			lab.setSourceType("app");
			lab.setClientIp(ip);
			lac.setData(false,lab);
			lac.callService();
			Logger.verifyEquals(ECode.AUTH_USER_ISLOCKED, lac.getMsg(), "验证登录返回消息");
			//JIRACONN-3031 锁定删除token
			Logger.verifyIsNull(new UserTokenWapper().getUserTokenByUid(UserId),"验证token被删除");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_LockUserRequestDto_002() {
		Logger.start(true,"锁定多个用户,验证用户登录提示用户已锁定");
		try {		
			Logger.comment("注册一个新用户");
			ArrayList<Integer> userIds=new ArrayList<Integer>();
			ArrayList<String> userNames=new ArrayList<String>();
			String ip=TestDataManager.getRandomIp();
			for (int i=0;i<3;i++){
				Logger.comment("注册一个新用户");
				String email=TestDataManager.getEmail();
				String phone=TestDataManager.getPhoneNum();
				Map UserMap =UserCallService.registerUser(email, phone, ip);
				String AccessToken = (String) UserMap.get("token");
				int UserId = (Integer) UserMap.get("userId");
				String UserName = (String) UserMap.get("loginId");
				userIds.add(UserId);
				userNames.add(UserName);
			}	
			Logger.comment("锁住用户");
			lockuserrequestdtoBean.setUserId(userIds);
			lockuserrequestdtoBean.setDescription("autotest");
			lockuserrequestdtoBean.setOperating("auto");
			lockuserrequestdtoCall.setData(lockuserrequestdtoBean);
			lockuserrequestdtoCall.callService();			
			//登录
			LoginAuthBean lab = new LoginAuthBean();
			LoginAuthCall lac = new LoginAuthCall();
			Logger.comment("开始登陆，验证用户被锁定");
			for (String name:userNames){
				lab.setLoginId(name);
				lab.setPas("123456a");
				lab.setSourceType("app");
				lab.setClientIp(ip);
				lac.setData(false,lab);
				lac.callService();
				Logger.verifyEquals(ECode.AUTH_USER_ISLOCKED, lac.getMsg(), "验证"+name+"登录返回用户锁定");		
			}

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_LockUserRequestDto_003() {
		Logger.start(false,"用户id为空");
		try {			
			lockuserrequestdtoBean.setDescription("autotest");
			lockuserrequestdtoBean.setOperating("auto");
			lockuserrequestdtoCall.setData(lockuserrequestdtoBean);
			lockuserrequestdtoCall.callService();			
			Logger.verifyEquals(ECode.AUTH_NOUSERID,lockuserrequestdtoCall.getMsg(), "验证提示信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_LockUserRequestDto_004() {
		Logger.start(false,"操作为空");
		try {			
			lockuserrequestdtoBean.setUserId(new ArrayList<Integer>());
			lockuserrequestdtoBean.setDescription("autotest");
			lockuserrequestdtoCall.setData(lockuserrequestdtoBean);
			lockuserrequestdtoCall.callService();			
			Logger.verifyEquals(ECode.AUTH_NOOPERATION_DESCRIPTION,lockuserrequestdtoCall.getMsg(), "验证提示信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_LockUserRequestDto_005() {
		Logger.start(false,"操作描述为空");
		try {			
			lockuserrequestdtoBean.setUserId((ArrayList<Integer>)(new ArrayList<Integer>()));
			lockuserrequestdtoBean.setDescription("autotest");
			lockuserrequestdtoCall.setData(lockuserrequestdtoBean);
			lockuserrequestdtoCall.callService();			
			Logger.verifyEquals(ECode.AUTH_NOOPERATION_DESCRIPTION,lockuserrequestdtoCall.getMsg(), "验证提示信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}